package com.spring.thread;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * ClassName: TaskService
 * Description:
 * Date: 2017/5/11 21:52
 *
 * @author SAM SHO
 * @version V1.0
 */
@Service
public class TaskService {

    /**
     * 没有返回值
     */
    @Async
    public void asyncMethodWithVoidReturnType() {
        System.out.println("Execute method asynchronously. "
                + Thread.currentThread().getName());
    }

    /**
     * 带返回值
     *
     * @return Future<String>
     */
    @Async
    public Future<String> asyncMethodWithReturnType() {
        System.out.println("Execute method asynchronously - "
                + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
            return new AsyncResult<>("hello world !!!!");
        } catch (InterruptedException e) {
        }
        return null;
    }
}
